<script setup>
import { ref } from 'vue'
import useUserInfoStore from '@/stores/userInfo.js'

import {useTokenStore} from '@/stores/token.js';

const userInfoStore = useUserInfoStore();
const userInfo = ref({...userInfoStore.info})
const tokenStore = useTokenStore();
const uploadRef = ref()


const rules = {
    name: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        {
            pattern: /^\S{2,10}$/,
            message: '昵称必须是2-10位的非空字符串',
            trigger: 'blur'
        }
    ],
    email: [
        { required: true, message: '请输入用户邮箱', trigger: 'blur' },
        { type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
    ],
    phone: [
        { required: true, message: '请输入用户电话号码', trigger: 'blur' },
    ],
    identifyNumber: [
        { required: true, message: '请输入身份证号', trigger: 'blur' },
    ],
}


//用户头像地址
const imgUrl= ref(userInfo.value.image)

//修改个人信息
import {userInfoUpdateService} from '@/api/admin.js'
import {ElMessage} from 'element-plus'
const updateUserInfo = async ()=>{
    
    userInfo.value.image = imgUrl.value
    //调用接口
    let result = await userInfoUpdateService(userInfo.value);
    ElMessage.success(result.msg? result.msg : '修改成功');
    
    //修改pinia中的个人信息
    userInfoStore.setInfo(userInfo.value)
}


//图片上传成功的回调函数
const uploadSuccess = (result)=>{
    console.log(result)
    if(result.code === 1){
        imgUrl.value = result.data
    }
}

</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>基本资料</span>
            </div>
        </template>
        <el-row>
            <el-col :span="12">
            
                <el-form :model="userInfo" :rules="rules" label-width="100px" size="large">
                    <el-form-item label="头像">
                        <el-upload 
                            ref="uploadRef"
                            class="avatar-uploader" 
                            :show-file-list="false"
                            :auto-upload="true"
                            accept="image/png, image/jpeg"
                            action="/api/upload"
                            name="file"
                            :headers="{'Token':tokenStore.token}"
                            :on-success="uploadSuccess"
                            >
                            <img :src="imgUrl ? imgUrl : userInfo.image" class="avatar" />
                        </el-upload>
                        <hr />
                        <el-button type="primary" :icon="Plus" size="large"  @click="uploadRef.$el.querySelector('input').click()">
                            选择图片
                        </el-button>
                    </el-form-item>
                    <el-form-item label="ID">
                        <el-input v-model="userInfo.id" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="用户名" prop="nickname">
                        <el-input v-model="userInfo.name"></el-input>
                    </el-form-item>
                    <el-form-item label="电话号码" prop="phone">
                        <el-input v-model="userInfo.phone"></el-input>
                    </el-form-item>
                    <el-form-item label="邮箱" prop="email">
                        <el-input v-model="userInfo.email"></el-input>
                    </el-form-item>
                    <el-form-item label="性别" prop="sex">
                        <el-radio-group v-model="userInfo.sex">
                            <el-radio label="男" value="'男'"></el-radio>
                            <el-radio label="女" value="'女'"></el-radio>
                            <el-radio label="其他" :value="'其他'"></el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="身份证号" prop="identifyNumber">
                        <el-input v-model="userInfo.identifyNumber"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="updateUserInfo">提交修改</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </el-card>
</template>

<style lang="scss" scoped>
.avatar-uploader {
:deep() {
    .avatar {
        width: 150px;
        height: 150px;
        display: block;
    }

    .el-upload {
        border: 1px dashed var(--el-border-color);
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
        transition: var(--el-transition-duration-fast);
    }

    .el-upload:hover {
        border-color: var(--el-color-primary);
    }

    .el-icon.avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 278px;
        height: 278px;
        text-align: center;
    }
}
}
</style>